package JavaBeans;

import java.util.Arrays;

/**
 * 书籍管理类：包含书籍对象数组和对该数组操作的方法
 */
public class BookList {
    public static final int DEFAULT_SIZE = 5;
    public static final int INCREASE_SIZE = 3;

    private int currentCount = 0;
    private Book[] books;

    public BookList() {
        books = new Book[DEFAULT_SIZE]; //创建图书对象数组
        books[0] = new Book("Java编程思想","Bruce Eckel",39.9,"编程语言");
        books[1] = new Book("Java核心技术（卷一）","Cay S.Horstmann",48.9,"编程语言");
        books[2] = new Book("Java核心技术（卷二）","Cay S.Horstmann",49.9,"编程语言");
        books[3] = new Book("数据结构与算法分析","Mark Allen Weiss",39.9,"数据结构");
        currentCount = 4;
    }

    /**
     * 扩容数组
     */
    public void checkCapacities(){
        if(currentCount == books.length){ //当实际图书个数等于数组长度时扩容
            books = Arrays.copyOf(books,books.length + INCREASE_SIZE);
            System.out.println("自动扩容成功，当前容量大小：" + books.length);
        }
    }


    //换一种对图书对象数组增删改查的方法，使用面向对象思想，将每个功能都通过对象来操作


    public int getCurrentCount() {
        return currentCount;
    }

    public void setCurrentCount(int currentCount) {
        this.currentCount = currentCount;
    }

    public Book getBook(int index){
        return books[index];
    }

    public void setBook(int index,Book book){
        books[index] = book;
    }
}
